package com.it.controller.web;

import com.it.constant.JwtClaimsConstant;
import com.it.dto.EmployeeLoginDTO;
import com.it.dto.UserLoginDTO;
import com.it.entity.Employee;
import com.it.entity.Users;
import com.it.mapper.UserMapper;
import com.it.properties.JwtProperties;
import com.it.result.Result;
import com.it.service.UserService;
import com.it.utils.JwtUtil;
import com.it.vo.EmployeeLoginVO;
import com.it.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wenjunjay
 * @Date: 2025/02/21/下午5:59
 * @Description:
 */

@RestController
@RequestMapping("/web/user")
@Slf4j
@Api(tags = "用户控制类")
public class UserController {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserService userService;


    //注册
    @PostMapping("/register")
    @ApiOperation( value = "注册")
    public Result<UserLoginVO> register(@RequestBody UserLoginDTO dto) {
        log.info("用户注册：{}", dto);
        userService.register(dto);
        return Result.success();
    }

    //登录接口
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO dto) {
        log.info("用户登录：{}", dto);

         Users user=userService.login(dto);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getUserId())
                .userName(user.getUsername())
                .name(user.getUsername())
                .token(token)
                .build();
        System.out.println(userLoginVO);
        return Result.success(userLoginVO);
    }

    @GetMapping("/text")
    public Result<String> text() {

        return Result.success();
    }

}
